const moment = require('moment');
const consts = require('../../../util/consts');
const oilLog = require('../../../domain/oilLog');
const moneyLog = require('../../../domain/moneyLog');

module.exports = function (app) {
    return new Handler(app);
};
var Handler = function (app) {
    this.app = app;
};
var handler = Handler.prototype;

/**
 * 宝石明细列表
 * @param msg
 * @param session
 * @param next
 */
handler.getOilDetail = function (msg, session, next) {
    var self = this;
    var uid = session.get('uid');
    var pageNum = msg.pageNum || 1;
    var pageSize = msg.pageSize || 10;
    pageSize = parseInt(pageSize);
    var redis = self.app.get('redis');
    if (pageSize > 20) {
        pageSize = 20;
    }
    //宝石明细缓存key
    let cacheKey = consts.RedisPrefix.OIL_DETAIL_PREFIX + uid;
    //页码缓存
    let pageCache = consts.RedisPrefix.PAGE_PREFIX + pageNum;

    // 判断uid是否为空
    if (!uid) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.MISSING_PARAMETER
        });
        return;
    }
    //判断是否有缓存数据
    redis.exists(cacheKey, function (err, data) {
        if (data != 0) { //有缓存
            redis.hexists(cacheKey, pageCache, function (err, data) {
                if (data != 0) {
                    redis.hget(cacheKey, pageCache, function (err, data) {
                        let result = JSON.parse(data);
                        //返回缓存中的数据
                        next(null, {
                            code: consts.Code.SUC,
                            data: result
                        });
                        return;
                    });
                } else {
                    oilLog.find({uid: uid}, function (err, data) {
                        if (err) {
                            next(null, {
                                code: consts.Code.ERR,
                                msg: err
                            });
                            return
                        }
                        var result = [];
                        for (var i = 0; i < data.length; i++) {
                            result.push({
                                increase_oil: Number(data[i].increase_oil).toFixed(2),
                                total_oil: Number(data[i].total_oil).toFixed(2),
                                type: data[i].type,
                                time: moment(data[i].gmt_create).format('YYYY-MM-DD HH:mm'),
                            });
                        }
                        next(null, {
                            code: consts.Code.SUC,
                            data: result
                        });
                        //设置缓存数据
                        redis.hset(cacheKey, pageCache, JSON.stringify(result), function (err, data) {
                            redis.expire(cacheKey, 60, function (err, data) {
                            });
                        });
                        return;
                    }).sort({gmt_create: -1}).skip((pageNum - 1) * pageSize).limit(pageSize);
                }
            });
        } else { //没有缓存
            oilLog.find({uid: uid}, function (err, data) {
                if (err) {
                    next(null, {
                        code: consts.Code.ERR,
                        msg: err
                    });
                    return
                }
                var result = [];
                for (var i = 0; i < data.length; i++) {
                    result.push({
                        increase_oil: Number(data[i].increase_oil).toFixed(2),
                        total_oil: Number(data[i].total_oil).toFixed(2),
                        type: data[i].type,
                        time: moment(data[i].gmt_create).format('YYYY-MM-DD HH:mm'),
                    });
                }
                next(null, {
                    code: consts.Code.SUC,
                    data: result
                });
                //设置缓存数据
                redis.hset(cacheKey, pageCache, JSON.stringify(result), function (err, data) {
                    redis.expire(cacheKey, 60, function (err, data) {
                    });
                });
                return;
            }).sort({gmt_create: -1}).skip((pageNum - 1) * pageSize).limit(pageSize);
        }
    });
};

/**
 * 收益明细列表
 * @param msg
 * @param session
 * @param next
 */
handler.getMoneyDetail = function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    let pageNum = msg.pageNum || 1;
    let pageSize = msg.pageSize || 10;
    pageSize = parseInt(pageSize);
    let redis = self.app.get('redis');
    if (pageSize > 15) {
        pageSize = 15;
    }
    //收益明细缓存key
    let cacheKey = consts.RedisPrefix.MONEY_DETAIL_PREFIX + uid;
    //页码缓存
    let pageCache = consts.RedisPrefix.PAGE_PREFIX + pageNum;

    // 判断uid是否为空
    if (!uid) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.MISSING_PARAMETER
        });
        return;
    }
    //判断是否有缓存数据
    redis.exists(cacheKey, function (err, data) {
        if (data != 0) { //有缓存
            redis.hexists(cacheKey, pageCache, function (err, data) {
                if (data != 0) {
                    redis.hget(cacheKey, pageCache, function (err, data) {
                        let result = JSON.parse(data);
                        //返回缓存中的数据
                        next(null, {
                            code: consts.Code.SUC,
                            data: result
                        });
                        return;
                    });
                } else {
                    moneyLog.find({uid: uid}, function (err, data) {
                        if (err) {
                            next(null, {
                                code: consts.Code.ERR,
                                msg: err
                            });
                            return
                        }
                        let result = [];
                        for (let i = 0; i < data.length; i++) {
                            result.push({
                                increase_money: Number(data[i].increase_money).toFixed(2),
                                type: data[i].type,
                                time: moment(data[i].gmt_create).format('YYYY-MM-DD HH:mm'),
                            });
                        }
                        next(null, {
                            code: consts.Code.SUC,
                            data: result
                        });
                        //设置缓存数据
                        redis.hset(cacheKey, pageCache, JSON.stringify(result), function (err, data) {
                            redis.expire(cacheKey, 60, function (err, data) {
                            });
                        });
                        return;
                    }).sort({gmt_create: -1}).skip((pageNum - 1) * pageSize).limit(pageSize);
                }
            });
        } else { //没有缓存
            moneyLog.find({uid: uid}, function (err, data) {
                if (err) {
                    next(null, {
                        code: consts.Code.ERR,
                        msg: err
                    });
                    return
                }
                let result = [];
                for (let i = 0; i < data.length; i++) {
                    result.push({
                        increase_money: Number(data[i].increase_money).toFixed(2),
                        type: data[i].type,
                        time: moment(data[i].gmt_create).format('YYYY-MM-DD HH:mm'),
                    });
                }
                next(null, {
                    code: consts.Code.SUC,
                    data: result
                });
                //设置缓存数据
                redis.hset(cacheKey, pageCache, JSON.stringify(result), function (err, data) {
                    redis.expire(cacheKey, 60, function (err, data) {
                    });
                });
                return;
            }).sort({gmt_create: -1}).skip((pageNum - 1) * pageSize).limit(pageSize);
        }
    });
}
